package com.hospital.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.hospital.domain.BloodProductType;
import com.hospital.domain.BloodTransfusionRecord;
import com.hospital.domain.DBloodApplyRecord;
import com.hospital.domain.Doctor;
import com.hospital.service.BloodProductTypeService;
import com.hospital.service.DBloodApplyRecordService;
import com.hospital.service.DoctorService;
import com.hospital.utils.R;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.CachePut;
import org.springframework.web.bind.annotation.*;

import java.text.SimpleDateFormat;
import java.util.Arrays;
import java.util.Date;
import java.util.List;
import java.util.logging.Handler;

@Slf4j
@RestController
@RequestMapping("/dbloodapplyrecord")
@CrossOrigin(origins = "*",maxAge = 3600)
public class DBloodApplyRecordController {

    @Autowired
    private DBloodApplyRecordService dBloodApplyRecordService;

    @Autowired
    private DoctorService doctorService;

    @Autowired
    private BloodProductTypeService bloodProductTypeService;

    @GetMapping("/wait/selectPage")
    public R selectPage(@RequestParam(defaultValue = "1") Integer pageNum,
                         @RequestParam(defaultValue = "3") Integer pageSize){
        Page<DBloodApplyRecord> page = new Page<>(pageNum,pageSize);
        LambdaQueryWrapper<DBloodApplyRecord> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(DBloodApplyRecord::getState,"等待");
        Page<DBloodApplyRecord> dBloodApplyRecordPage = dBloodApplyRecordService.page(page,queryWrapper);
        List<DBloodApplyRecord> records = dBloodApplyRecordPage.getRecords();

        LambdaQueryWrapper<DBloodApplyRecord> qw = new LambdaQueryWrapper<>();
        qw.eq(DBloodApplyRecord::getState,"等待");
        int total=dBloodApplyRecordService.count(qw);

        for(DBloodApplyRecord record : records){
            int doctorId = record.getDoctorId();
            Doctor doctor = doctorService.getById(doctorId);
            int bloodProductTypeId = record.getBloodProductTypeId();
            BloodProductType bloodProductType = bloodProductTypeService.getById(bloodProductTypeId);
            record.setDoctorName(doctor.getName());
            record.setBloodProductName(bloodProductType.getProductName());
        }
        return R.ok().put("data",records).put("total",total);
    }

    @GetMapping("/allow/selectPage")
    public R selectPageWait(@RequestParam(defaultValue = "1") Integer pageNum,
                            @RequestParam(defaultValue = "3") Integer pageSize){
        Page<DBloodApplyRecord> page = new Page<>(pageNum,pageSize);
        LambdaQueryWrapper<DBloodApplyRecord> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.ne(DBloodApplyRecord::getState,"等待");
        Page<DBloodApplyRecord> dBloodApplyRecordPage = dBloodApplyRecordService.page(page,queryWrapper);
        List<DBloodApplyRecord> records = dBloodApplyRecordPage.getRecords();

        LambdaQueryWrapper<DBloodApplyRecord> qw = new LambdaQueryWrapper<>();
        qw.ne(DBloodApplyRecord::getState,"等待");
        int total=dBloodApplyRecordService.count(qw);

        for(DBloodApplyRecord record : records){
            int doctorId = record.getDoctorId();
            Doctor doctor = doctorService.getById(doctorId);
            int bloodProductTypeId = record.getBloodProductTypeId();
            BloodProductType bloodProductType = bloodProductTypeService.getById(bloodProductTypeId);
            record.setDoctorName(doctor.getName());
            record.setBloodProductName(bloodProductType.getProductName());
        }
        return R.ok().put("data",records).put("total",total);
    }

//    @CachePut(value = "bloodApplyRecord",key = "#id")
    @PostMapping("/updateState1/{id}")
    public R updateState1(@PathVariable int id){
        DBloodApplyRecord dBloodApplyRecord = dBloodApplyRecordService.getById(id);
        dBloodApplyRecord.setState("已批准");
        dBloodApplyRecordService.updateById(dBloodApplyRecord);
        return R.ok();
    }

//    @CachePut(value = "bloodApplyRecord",key = "#id")
    @PostMapping("/updateState2/{id}")
    public R updateState2(@PathVariable int id){
        DBloodApplyRecord dBloodApplyRecord = dBloodApplyRecordService.getById(id);
        dBloodApplyRecord.setState("未批准");
        dBloodApplyRecordService.updateById(dBloodApplyRecord);
        return R.ok();
    }

//    @CachePut(value = "bloodApplyRecord",key = "#id")
    @PostMapping("save")
    public R save(@RequestBody DBloodApplyRecord dBloodApplyRecord){
        dBloodApplyRecordService.save(dBloodApplyRecord);
        return R.ok();
    }

//    @CacheEvict(value = "bloodApplyRecord",key = "#id")
    @DeleteMapping("/delete/{id}")
    public R delete(@PathVariable int id){
        dBloodApplyRecordService.removeById(id);
        return R.ok();
    }

    @GetMapping("getRecordByHospital")
    public R getRecordByHospital() {
        List<DBloodApplyRecord> records = dBloodApplyRecordService.list();
        Double[] mount = new Double[7];
        Arrays.fill(mount, 0.0);
        for(DBloodApplyRecord record : records){
            if(record.getHospitalId() != 1) continue;
            Date date = record.getTransfusionDate();
            SimpleDateFormat monthFormat = new SimpleDateFormat("MM");
            String month = monthFormat.format(date);
            if (month.equals("05")) mount[0]+=record.getVolume();
            else if (month.equals("06")) mount[1]+=record.getVolume();
            else if (month.equals("07")) mount[2]+=record.getVolume();
            else if (month.equals("08")) mount[3]+=record.getVolume();
            else if (month.equals("09")) mount[4]+=record.getVolume();
            else if (month.equals("10")) mount[5]+=record.getVolume();
            else if (month.equals("11")) mount[6]+=record.getVolume();
        }
        return R.ok().put("data",mount);
    }
}
